package com.zys.hotel_management.controller;

import com.zys.hotel_management.common.Response;
import com.zys.hotel_management.entity.Admin;
import com.zys.hotel_management.entity.dto.LoginDTO;
import com.zys.hotel_management.service.AdminService;
import com.zys.hotel_management.utils.JwtUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理员控制器，处理与管理员相关的 HTTP 请求
 *
 * @author Soyo
 * @since 2025/5/23
 */
@RestController
@RequestMapping("/api/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    @PostMapping("/login")
    public Response<String> login(@RequestBody LoginDTO loginDTO) {
        Admin admin = adminService.login(loginDTO.getUsername(), loginDTO.getPassword());
        if (admin != null) {
            String jwtToken = JwtUtils.generateToken(admin.getUsername());
            return Response.success(jwtToken);
        } else {
            return Response.fail(401, "用户名或密码错误");
        }
    }
}
